/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.profile.luminosity.internal;

import java.util.Arrays;
import java.util.Collection;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.drools.runtime.rule.FactHandle;
import org.osami.gateway.profile.IProfileService;
import org.osami.gateway.profile.luminosity.LuminosityProfile;
import org.ow2.jasmine.rules.api.IDroolsWorkingMemory;
import org.ow2.jasmine.rules.api.IDroolsWorkingMemoryPool;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 14 juin 2010
 * Time: 09:29:36
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
@Instantiate
public class LuminosityProfileAdapter implements IProfileService {

    @Requires
    private IDroolsWorkingMemoryPool pool;

    @ServiceProperty(value = "luminosity")
    private String type;

    private LuminosityProfile profile;

    private FactHandle handle;

    @Validate
    public void start() throws Exception {
        profile = new LuminosityProfile();
        IDroolsWorkingMemory wm =  pool.getOrCreate("default");
        // Maybe keeping the FactHandle is not necessary
        handle = wm.insert(profile);
    }

    public String setParameter(String name, String value) {
        double old = 0;

        if ("topValue".equals(name)) {
            old = profile.getTopValue();
            profile.setTopValue(Double.valueOf(value));
        } else if ("bottomValue".equals(name)) {
            old = profile.getBottomValue();
            profile.setBottomValue(Double.valueOf(value));
        } else if ("plugId".equals(name)) {
            String id = profile.getPlugId();
            profile.setPlugId(value);
            return id;
        }

        return Double.toString(old);
    }

    public String getParameter(String name) {

        if ("topValue".equals(name)) {
            return Double.toString(profile.getTopValue());
        } else if ("bottomValue".equals(name)) {
            return Double.toString(profile.getBottomValue());
        }else if ("plugId".equals(name)) {
            return profile.getPlugId();
        }

        return null;
    }

    public Collection<String> getParameters() {
        return Arrays.asList("bottomValue", "topValue", "plugId");
    }
}
